package leoner.multithread2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class RunnableTester {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		PrintTask task1 = new PrintTask("t1");
		PrintTask task2 = new PrintTask("t2");
		PrintTask task3 = new PrintTask("t3");
		Map<String,Object> maps = new HashMap<String,Object>();
		MockService service = new MockServiceImpl();
		
		Callable t1 = new PrintTask2<String>(service,"m1",maps,1,"kang");
		Callable t2 = new PrintTask2<String>(service,"m2",maps,2);
		Callable t3 = new PrintTask2<String>(service,"m3",maps,3,21,"hui");
		Callable t4 = new PrintTask2<String>(service,"m4",maps,"+");
	
		
		
		System.out.println("Start Threads");
		ExecutorService threadExcutor = Executors.newFixedThreadPool(3);
		List<Callable<String>> tasks = new ArrayList<Callable<String>>(3);
		
		tasks.add(t1);
		tasks.add(t2);
		tasks.add(t3);
		tasks.add(t4);
		
//		threadExcutor.execute(task1);
//		threadExcutor.execute(task2);
//		threadExcutor.execute(task3);
		threadExcutor.invokeAll(tasks);
//		List<Future<String>> fs  = threadExcutor.invokeAll(tasks);
//		for(Future<String> f : fs){
//			System.out.println("f---->"+f.get());
//		}
		for(String key : maps.keySet()){
			System.out.println("key:"+key+",value:"+maps.get(key));
		}
		threadExcutor.shutdown();
		
		System.out.println("Threads started , main ends\n");
		
	}
}
